package framework.dao.imp;

import framework.dao.ItemDao;
import framework.pojo.Item;

import java.util.List;

public class ItemDaoImp extends BaseDao implements ItemDao {
    @Override
    public int addItem(Item item) {
        String sql = "insert into a_item(`name`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?)";

        return update(sql, item.getName(), item.getPrice(), item.getSales(), item.getStock(), item.getImgPath());
    }

    @Override
    public int deleteItemById(Integer id) {
        String sql = "delete from a_item where id = ?";
        return update(sql, id);
    }

    @Override
    public int updateItem(Item item) {
        String sql = "update a_item set `name`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id = ?";
        return update(sql,item.getName(),item.getPrice(),item.getSales(),item.getStock(),item.getImgPath(),item.getId());
    }

    @Override
    public Item queryItemById(Integer id) {
        String sql = "select `id` , `name` , `price` , `sales` , `stock` , `img_path` imgPath from a_item where id = ?";
        return queryForOne(Item.class, sql,id);
    }

    @Override
    public List<Item> queryItems() {
        String sql = "select `id` , `name` ,  `price` , `sales` , `stock` , `img_path` imgPath from a_item";
        return queryForList(Item.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from a_item";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Item> queryForPageItems(int begin, int pageSize) {
        String sql = "select `id` , `name` , `price` , `sales` , `stock` , `img_path` imgPath from a_item limit ?,?";
        return queryForList(Item.class,sql,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from a_item where price between ? and ?";
        Number count = (Number) queryForSingleValue(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Item> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath " +
                "from a_item where price between ? and ? order by price limit ?,?";
        return queryForList(Item.class,sql,min,max,begin,pageSize);
    }
}
